﻿/**
*author:liubing
*date:2011-6-15
*modify:wujiannan on 2011-6-17
*history:
**/
function HTMLDecode(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
}

var tmpDialog = null;
function showDialog(title,msg) {
    var boxContent = "<img src=\"http://static.51shumi.com/global/artDialog/css/shumi/load.gif\" id='imgLoading'/><span id='spMsg' style='color:orange;font-size:14px; margin-left:5px;  text-align:center; font-weight:bold'>" + msg + "</span><input type='hidden' id='hidLink'></input>";
    tmpDialog = $.dialog({
        id: "msgBox",
        title: title,
        content: boxContent,
        yesFn: null,
        border: false,
        lock: true,
        width: 220,
        height: 80,
        window: "parent",
        noFn: null,
        skin: ['shumi']
    });
}
var jsonData = $.parseJSON(parameters); //parameters定义在控件页面上
var ucEditor = {
    createEditor: function (containerID) {
        $(containerID).attr("onclick", "");
        $(containerID).removeAttr("style");
        //1、得到编辑器UI
        var NameObj = editInstance.CreateEditID();
        var EditHtml = editInstance.GetEditHtml(NameObj);
        $(containerID).html(EditHtml);
        //2、得到编辑器功能   
        FundEditorInst = new EditorClass("FundEditorInst", NameObj);
        FundEditorInst.Init();
        FundEditorInst.QuoteFocue();
    },

    initForms: function () {
        this.createEditor("#div_bbs_editor");
        this.bindClick();
    },

    validateData: function () {
        var title = $.trim($("#div_bbs_editor_title").val());
        var content = $.trim(FundEditorInst.GetHContent());
        if (title == null || title == undefined || title == "") {
            alert("帖子主题不能为空!");
            return false;
        }
        if (content == null || content == undefined || content == "") {
            alert("帖子内容不能为空!");
            return false;
        }
        if (title.length < 4) {
            alert("帖子标题不能少于4个字!");
            $("#div_bbs_editor_title").focus();
            return false;
        }
        if (content.length < 2) {
            alert("帖子内容不能少于2个字!");
            return false;
        }
        return true;
    },
    bindClick: function () {
        $("#sendPost").click(function () {
            if (ucEditor.validateData()) {
                showDialog("提示", "操作正在进行中...");
                $.post(
					"AjaxHandler/PublishPostHandler.ashx",
					ucEditor.buildPostbackData(),
					function (result) {
					    var jsonResult = $.parseJSON(result);
					    if (jsonResult.IsSucceed) {
					        var url = "/";
					        if (jsonData.HrefUrl) {
					            url = jsonData.HrefUrl;
					            if (jsonResult.Index) {
					                url = url + jsonResult.PostId + "&index=" + jsonResult.Index + "&t="+new Date().getTime()+"#" + jsonResult.ReplyFloor;
					            }
					            else {
					                url = url + jsonResult.PostId;
					            }
					        }
					        $("#imgLoading").hide();
					        $("#spMsg").html(jsonResult.SuccessMsg + "<br/>正在跳转页面...");
					        setTimeout("tmpDialog.close();", 1000 * 3);
					        window.location.href = url; //锚点在本页时，不会刷新页面。需要用时间区分url。
					    }
					    else {
					        $("#imgLoading").hide();
					        $("msgBoxtitle").html("警告");
					        $("#spMsg").text(jsonResult.ErrorMsg);
					        setTimeout("tmpDialog.close();", 1000 * 3);
					        return;
					    }
					}
				);
            }
        });
    },
    buildPostbackData: function () {
        title = $("#div_bbs_editor_title").val();
        content = FundEditorInst.GetHContent();
        switch (jsonData.UseType.toLowerCase()) {
            case "post":
                return {
                    "BoardId": jsonData.BoardId,
                    "PostId": jsonData.PostId,
                    "ReplyTitle": title,
                    "ReplyContent": content,
                    "IsReply": jsonData.IsReply,
                    "ReplyId": jsonData.ReplyId,
                    "Operate": jsonData.Operate,
                    "QuoteId": UCEditor_quoteId
                };
                break;
            case "board":
                return {
                    "BoardId": jsonData.BoardId,
                    "PostTitle": title,
                    "PostContent": content,
                    "Operate": jsonData.Operate,
                    "IsReply": jsonData.IsReply
                };
                break;
            case "uniform":
                return {
                    "BoardId": jsonData.BoardId,
                    "PostId": jsonData.PostId,
                    "PostTitle": title,
                    "PostContent": content,
                    "IsReply": jsonData.IsReply,
                    "ReplyId": jsonData.ReplyId,
                    "Operate": jsonData.Operate
                };
                break;
        }
    }
}
ucEditor.initForms();
function sendReply() {
    $("#sendPost").click();
}
